#include "PlatformProperty.h"

static bool isMoveLimited(int32_t pos) {
    int32_t min = lcfg::LocalConfig::getInstance().getInt<int32_t>(lcfg::LCFG_Min_Backrest_angle);
    int32_t max = lcfg::LocalConfig::getInstance().getInt<int32_t>(lcfg::LCFG_Max_Backrest_angle);
    if (pos < min || pos > max) {
        VHALOG.d("SEAT_BACKREST_ANGLE_1_MOVE isMoveLimited pos=%d, min=%d, max=%d", pos, min, max);
        return true;
    }
    return false;
}

// 3.2.9 [SW_FC_004_046]第一排靠背调节1stRowSeatBackrestAdjustment
SYSTEM_READ_PROPERTY(SEAT_BACKREST_ANGLE_1_POS, int32_t) {
    builder.area(VehicleAreaSeat::ROW_1_LEFT)
        .initValue([]() { return 0; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                VHALOG.d("SEAT_BACKREST_ANGLE_1_POS drvr cc472[%d], cc1173[%d], cc184[%d]",
                         SIGNAL.getcarconfig472(), SIGNAL.getcarconfig1173(), SIGNAL.getcarconfig184());
                if (SIGNAL.carConfigIs(SignalRX::carconfig471,
                                       sdb::CarConfig471::_02,
                                       sdb::CarConfig471::_03) &&
                    SIGNAL.carConfigIs(SignalRX::carconfig1173,
                                       sdb::CarConfig1173::_02) &&
                    SIGNAL.carConfigIs(SignalRX::carconfig184,
                                       sdb::CarConfig184::_02,
                                       sdb::CarConfig184::_03,
                                       sdb::CarConfig184::_04)) {
                    VHALOG.d("SEAT_BACKREST_ANGLE_1_POS drvr Active");
                    return PropertyStatus::Active;
                }
                VHALOG.d("SEAT_BACKREST_ANGLE_1_POS drvr NotAvailable");
                return PropertyStatus::NotAvailable;
            },
            SignalRX::carconfig471,
            SignalRX::carconfig1173,
            SignalRX::carconfig184)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t& last) {
                auto angle = SIGNAL.getSeatBackAngleRowFirstDrvr();
                VHALOG.d("SEAT_BACKREST_ANGLE_1_POS drvr SeatBackAngleRowFirstDrvr %d", angle);
                if (isMoveLimited(angle)) {
                    return last;
                }
                return angle;
            },
            SignalRX::SeatBackAngleRowFirstDrvr)
        .area(VehicleAreaSeat::ROW_1_RIGHT)
        .initValue([]() { return 0; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                VHALOG.d(
                    "SEAT_BACKREST_ANGLE_1_POS pass cc472[%d], cc1174[%d], cc184[%d]",
                    SIGNAL.getcarconfig472(), SIGNAL.getcarconfig1174(), SIGNAL.getcarconfig184());
                if (SIGNAL.carConfigIs(SignalRX::carconfig471,
                                       sdb::CarConfig471::_02) &&
                    SIGNAL.carConfigIs(SignalRX::carconfig1174,
                                       sdb::CarConfig1174::_02) &&
                    SIGNAL.carConfigIs(SignalRX::carconfig184,
                                       sdb::CarConfig184::_04)) {
                    VHALOG.d("SEAT_BACKREST_ANGLE_1_POS pass Active");
                    return PropertyStatus::Active;
                }
                VHALOG.d("SEAT_BACKREST_ANGLE_1_POS pass NotAvailable");
                return PropertyStatus::NotAvailable;
            },
            SignalRX::carconfig471,
            SignalRX::carconfig1174,
            SignalRX::carconfig184)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t last) {
                auto angle = SIGNAL.getSeatBackAngleRowFirstPass();
                VHALOG.d("SEAT_BACKREST_ANGLE_1_POS pass SeatBackAngleRowFirstPass %d", angle);
                if (isMoveLimited(angle)) {
                    return last;
                }
                return angle;
            },
            SignalRX::SeatBackAngleRowFirstPass);
}